package com.DongBing.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import com.DongBing.common.exception.DongBingException;
import com.DongBing.common.utils.Res;
import com.DongBing.common.valid.AddClass;
import lombok.RequiredArgsConstructor;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.DongBing.entity.BrandEntity;
import com.DongBing.service.BrandService;
import com.DongBing.common.utils.PageUtils;
import com.DongBing.common.utils.R;

import javax.validation.Valid;


/**
 * 品牌
 *
 * @author ¶­±ù
 * @email 861571118@QQ.com
 * @date 2022-05-16 15:56:01
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/product/brand")
public class BrandController {

    private final BrandService brandService;

    /**
     * 列表
     */
    @GetMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = brandService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{brandId}")
    public R info(@PathVariable("brandId") Long brandId){
		BrandEntity brand = brandService.getById(brandId);
        return R.ok().put("brand", brand);
    }

    /**
     * 保存
     * 测试参数校验
     */
    @PostMapping("/save")
    public Res<?> save(@Validated(value = AddClass.class) @RequestBody BrandEntity brand){
        val map = new HashMap<String,String>();
		brandService.save(brand);
        return Res.success("添加成功");
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody BrandEntity brand){
		brandService.updateById(brand);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] brandIds){
		brandService.removeByIds(Arrays.asList(brandIds));
        return R.ok();
    }

    @GetMapping(value = "/delete/{id}")
    public Res<?> delete(@PathVariable("id") String id){
        return brandService.delete(id);
    }

}
